package com.fingard.dsp.bank.directbank.vbao01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.MD5SaltHelper;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class VBAO01OtherElse extends VBAO01Base implements IOtherElse {
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		
		if ("VerifySign".equals(transType)) {
            // 一码付验签
            return verifyOneQRPay(p_reqElse);
        } else if ("UnifiedSign".equals(transType)) {
            if (!"POS".equals(p_reqElse.reqHeader.commandCode)) {
                return unifiedSign(p_reqElse);
            }
		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			
			return retAts;
		}
		return null;
	}
	
	public RetAtsOtherElse verifyOneQRPay(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();
		
		try {
			String ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
			ActSetItem actItem = getBankConfig().getActSetItemByAny(ownAct, "", "", "unifiedorder");
			if (actItem != null) {
				String plain = p_reqElse.reqHashMap.get("Plain");
				WriteBankLogLn2("微保一码付异步通知原文:\r\n" + plain);
				
				// 进行解密
				String oneQRPayOriRespXml = Base64vbao.decode(plain, getCharset());
				//oneQRPayOriRespXml = new String(oneQRPayOriRespXml.getBytes(), getCharset());
				WriteBankLogLn2("微保一码付异步通知解密后明文:\r\n" + oneQRPayOriRespXml);
				
				// 执行验签
				boolean verifySignResult = new VBAO01Base().verifySignature(oneQRPayOriRespXml, actItem.saltKey, getCharset());
				if (verifySignResult) {
					retAts.retHashMap.put("RespCode", "0");
					retAts.retHashMap.put("RespInfo", "验签通过");
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "验签失败");
				}
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}

    public RetAtsOtherElse unifiedSign(ReqOtherElse p_reqElse) {
        RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
        retAts.retHashMap = new HashMap<String, String>();

        try {
            Map<String, String> mapParams = p_reqElse.reqHashMap;

            ActSetItem actItem = getBankConfig().getActSetItem(p_reqElse.reqHashMap.get("BankAccNum"), "unifiedorder");
            if (actItem != null) {
                String saltKey = actItem.saltKey;
                WriteBankLogLn2("当前渠道MD5密钥为:[" + saltKey + "].");

                mapParams = removeHead(mapParams);

                String tmpSignData = getSignStr(mapParams);
                tmpSignData += "&key=" + saltKey;
                WriteBankLogLn2("待签名数据:\r\n" + tmpSignData);

                String sign = MD5SaltHelper.md5(tmpSignData, getCharset());
                WriteBankLogLn2("签名结果:" + sign);

                if (StringHelper.hasAnyChar(sign)) {
                    retAts.retHashMap.put("RespCode", "0");
                    retAts.retHashMap.put("RespInfo", "签名成功!");
                    retAts.retHashMap.put("Result", sign);
                } else {
                    retAts.retHashMap.put("RespCode", "-1");
                    retAts.retHashMap.put("RespInfo", "签名失败!");
                }
            } else {
                retAts.retHashMap.put("RespCode", "-1");
                retAts.retHashMap.put("RespInfo", "未找到账号:[" + p_reqElse.reqHashMap.get("OurBankAccount") + "]商户配置信息,请检查config.xml相关商户配置信息!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.retHashMap.put("RespCode", "-1");
            retAts.retHashMap.put("RespInfo", ex.getMessage());
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}
